package com.xiaoxie.system.authextend;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.xiaoxie.system.domain.User;
import com.xiaoxie.system.domain.Permission;
import com.xiaoxie.system.domain.Role;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        User user = StpUtil.getSession().getModel("user", User.class);
        if (user != null) {
            // 取出用户的权限封装为list返回
            return user.getPermissionList().stream().map(Permission::getCode).toList();
        }
        return null;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        User user = StpUtil.getSession().getModel("user", User.class);
        if (user != null) {
            // 取出用户的角色封装为list返回
            return user.getRoleList().stream().map(Role::getRole).toList();
        }
        return null;
    }
}
